import { getFloorInfo, getStorePage } from '../../../api/goods.js';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    allFloorvisible: false,
    floorArr: [],
    floorIndex: 0,
    storeList: [],
    pageInfo: {
      pageNo: 1,
      pageSize: 10,
    },
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.init();
  },
  async init() {
    const floorInfo = await getFloorInfo();
    const floorArr = [];
    for (const key in floorInfo) {
      floorArr.push({
        name: key,
        index: floorInfo[key],
      });
    }

    this.setData(
      {
        floorArr,
        buildingNo: floorArr ? floorArr[0].index : '',
      },
      () => {
        this.storeInfo();
      },
    );
  },
  chooseFloor(e) {
    const { index } = e.currentTarget.dataset;
    const buildingNo = e.currentTarget.dataset.buildingno;
    this.setData(
      {
        floorIndex: index,
        buildingNo,
        allFloorvisible: false,
      },
      () => {
        this.storeInfo(true);
      },
    );
  },
  async storeInfo(fresh = false) {
    if (fresh) {
      this.data.pageInfo.pageNo = 1;
    }
    const params = {
      ...this.data.pageInfo,
      buildingNo: this.data.buildingNo,
    };
    const storeInfo = await getStorePage(params);
    storeInfo.list.map(t => {
      t.products.map(u => {
        u.price = u.price ? (u.price / 100).toFixed(2) : '--'
        return u
      })      
    })
    this.setData({
      storeList: fresh ? storeInfo.list : this.data.storeList.concat(storeInfo.list),
    });
  },
  showAllFloor() {
    this.setData({
      allFloorvisible: true,
    });
  },
  hideAllFloor() {
    this.setData({
      allFloorvisible: false,
    });
  },
  onVisibleChange(e) {
    this.setData({
      allFloorvisible: e.detail.visible,
    });
  },

  onReachBottom() {
    this.data.pageInfo.pageNo++;
    this.storeInfo();
  },
});
